import Vue from 'vue'
import Router from 'vue-router'

/* Layout */
import Layout from '@/layout'

Vue.use(Router)
// 存储动态路由表的
export const asyncRoutes = [
  // 设置二级的路由的组件中的内容是显示在父组件中
  {
    path: '/departments',
    component: Layout,
    children: [
      {
        path: '', // 作为默认渲染路由
        name: 'departments',
        component: () => import('@/views/departments/departments.vue'),
        meta: { title: 'Departments', icon: 'departments' }
      }
    ]
  },
  {
    path: '/employees',
    component: Layout,
    children: [
      {
        path: '', // 默认自动匹配父路由
        name: 'employees',
        component: () => import('@/views/employees/employees.vue'),
        meta: { title: 'Employees', icon: 'employees' }
      }
    ]
  },
  {
    path: '/settings',
    component: Layout,
    children: [
      {
        path: '', // 默认自动匹配父路由
        name: 'settings',
        component: () => import('@/views/settings/settings.vue'),
        meta: { title: 'Settings', icon: 'settings' }
      }
    ]
  },
  {
    path: '/salarys',
    component: Layout,
    children: [
      {
        path: '', // 默认自动匹配父路由
        name: 'salarys',
        component: () => import('@/views/salarys/salarys.vue'),
        meta: { title: 'Salarys', icon: 'salarys' }
      }
    ]
  },
  {
    path: '/social_securitys',
    component: Layout,
    children: [
      {
        path: '', // 默认自动匹配父路由
        name: 'social_securitys',
        component: () => import('@/views/social_securitys/social_securitys.vue'),
        meta: { title: 'Social_securitys', icon: 'social_securitys' }
      }
    ]
  },
  {
    path: '/attendances',
    component: Layout,
    children: [
      {
        path: '', // 默认自动匹配父路由
        name: 'attendances',
        component: () => import('@/views/attendances/attendances.vue'),
        meta: { title: 'Attendances', icon: 'attendances' }
      }
    ]
  },
  {
    path: '/approvals',
    component: Layout,
    children: [
      {
        path: '', // 默认自动匹配父路由
        name: 'approvals',
        component: () => import('@/views/approvals/approvals.vue'),
        meta: { title: 'Approvals', icon: 'approvals' }
      }
    ]
  },
  {
    path: '/permissions',
    component: Layout,
    children: [
      {
        path: '', // 默认自动匹配父路由
        name: 'permissions',
        component: () => import('@/views/permissions/permissions.vue'),
        meta: { title: 'Permissions', icon: 'permissions' }
      }
    ]
  }

]

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
